<template>
  <div id="index">
    <div id="adminList" class="mod-config">
      <el-form id="admninFrom" :inline="true" :model="dataForm" @keyup.enter.native="getDataList()">
        <el-form-item>
          <el-input v-model="dataForm.key" placeholder="参数名" clearable></el-input>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
          <el-button type="danger" @click="deleteHandle()" :disabled="dataListSelections.length <= 0">批量删除</el-button>
        </el-form-item>
      </el-form>
      <el-table id="table" :data="dataList" border v-loading="dataListLoading"
        @selection-change="selectionChangeHandle">
        <el-table-column type="selection" header-align="center" align="center" width="50">
        </el-table-column>
        <el-table-column prop="id" header-align="center" align="center" label="id">
        </el-table-column>
        <el-table-column prop="projectCode" header-align="center" align="center" label="项目编号">
        </el-table-column>
        <el-table-column prop="testingLocation" header-align="center" align="center" label="检测地点">
        </el-table-column>
        <!-- <el-table-column
        prop="temperature1"
        header-align="center"
        align="center"
        label="环境温度1">
      </el-table-column>
      <el-table-column
        prop="temperature2"
        header-align="center"
        align="center"
        label="环境温度2">
      </el-table-column>
      <el-table-column
        prop="temperature3"
        header-align="center"
        align="center"
        label="环境温度3">
      </el-table-column> -->
        <el-table-column prop="temperatureRange" header-align="center" align="center" label="环境温度范围">
        </el-table-column>
        <!-- <el-table-column
        prop="humidity1"
        header-align="center"
        align="center"
        label="环境湿度1">
      </el-table-column>
      <el-table-column
        prop="humidity2"
        header-align="center"
        align="center"
        label="环境湿度2">
      </el-table-column>
      <el-table-column
        prop="humidity3"
        header-align="center"
        align="center"
        label="环境湿度3">
      </el-table-column> -->
        <el-table-column prop="humidityRange" header-align="center" align="center" label="环境湿度范围">
        </el-table-column>
        <!-- <el-table-column
        prop="detectPeople"
        header-align="center"
        align="center"
        label="检测人">
      </el-table-column>
      <el-table-column
        prop="recorders"
        header-align="center"
        align="center"
        label="记录人">
      </el-table-column>
      <el-table-column
        prop="checkers"
        header-align="center"
        align="center"
        label="校核人">
      </el-table-column>
      <el-table-column
        prop="pdfPath"
        header-align="center"
        align="center"
        label="pdf存放路径">
      </el-table-column>
      <el-table-column
        prop="wordPath"
        header-align="center"
        align="center"
        label="word存放路径">
      </el-table-column> -->
      <!-- <el-table-column
          prop="statusText"
          header-align="center"
          align="center"
          label="状态">
      </el-table-column> -->
        <el-table-column fixed="right" header-align="center" align="center" width="350" label="操作">
          <template slot-scope="scope">
            <el-button type="primary" size="small" @click="viewPdf(scope.row.id)">预览</el-button>
            <!-- <el-button type="text" size="small" v-if="[7].includes(scope.row.status)" @click="viewPdf(scope.row.id)">预览</el-button>
            <el-button type="text" size="small" v-if="[0,8,5,6,4].includes(scope.row.status)" @click="review(scope.row.id)">提交审核</el-button> -->
            <el-button type="primary" size="small" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button type="danger" size="small" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination id="page" @size-change="sizeChangeHandle" @current-change="currentChangeHandle"
        :current-page="pageIndex" :page-sizes="[10, 20, 50, 100]" :page-size="pageSize" :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper">
      </el-pagination>
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
  </div>
</template>

<script>
import AddOrUpdate from '../temperatureHumidityList/temperaturehumidity-add-or-update.vue'
export default {
  data() {
    return {
      dataForm: {
        key: ''
      },
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
      dataListSelections: [],
      addOrUpdateVisible: false
    }
  },
  components: {
    AddOrUpdate
  },
  activated() {
    this.getDataList()
  },
  methods: {
    review(id) {
        this.$http({
          url: this.$http.adornUrl(`/reportapproval/temperaturehumidity/review`),
          method: 'post',
          data: this.$http.adornData({
            'id': id,
          })
        }).then((data) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.visible = false
                this.getDataList();
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      },
    // 获取数据列表
    getDataList() {
      this.dataListLoading = true
      this.$http({
        url: this.$http.adornUrl('/reportapproval/temperaturehumidity/list'),
        method: 'get',
        params: this.$http.adornParams({
          'page': this.pageIndex,
          'limit': this.pageSize,
          'key': this.dataForm.key
        })
      }).then((data) => {
        if (data && data.code === 0) {
          this.dataList = data.page.list
          this.totalPage = data.page.totalCount
        } else {
          this.dataList = []
          this.totalPage = 0
        }
        this.dataListLoading = false
      })
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val
      this.pageIndex = 1
      this.getDataList()
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val
      this.getDataList()
    },
    // 多选
    selectionChangeHandle(val) {
      this.dataListSelections = val
    },
    // 新增 / 修改
    addOrUpdateHandle(id) {
      this.addOrUpdateVisible = true
      this.$nextTick(() => {
        this.$refs.addOrUpdate.init(id)
      })
    },
    // 删除
    deleteHandle(id) {
      var ids = id ? [id] : this.dataListSelections.map(item => {
        return item.id
      })
      this.$confirm(`确定对[id=${ids.join(',')}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$http({
          url: this.$http.adornUrl('/reportapproval/temperaturehumidity/delete'),
          method: 'post',
          data: this.$http.adornData(ids, false)
        }).then((data) => {
          if (data && data.code === 0) {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1500,
              onClose: () => {
                this.getDataList()
              }
            })
          } else {
            this.$message.error(data.msg)
          }
        })
      })
    },
      viewPdf(id) {
      // 显示加载中的转圈提示
      const loadingInstance = this.$loading({
        lock: true,
        text: '正在加载 PDF...',
        spinner: 'el-icon-loading',
        background: 'rgba(0, 0, 0, 0.7)'  // 可自定义背景颜色
      });

      // 发送请求获取 PDF URL
      this.$http({
        url: this.$http.adornUrl(`/reportapproval/temperaturehumidity/viewPdf/${id}`),
        method: 'GET',
        params: this.$http.adornParams()
      }).then((response) => {
        // 请求完成后关闭加载提示
        loadingInstance.close();
        // 成功获取到 PDF URL
        this.pdfUrl = response; // 假设返回的数据是 PDF URL
        this.openPdfInNewWindow(this.pdfUrl); // 打开 PDF 预览
      }).catch((error) => {
        // 请求失败时关闭加载提示
        loadingInstance.close();

        // 处理请求错误
        this.$message.error('网络错误，请稍后再试');
        console.error('请求失败：', error);
      });
    },

    // 打开新窗口预览 PDF
    openPdfInNewWindow(pdfUrl) {
      // 如果返回的 URL 是有效的，打开新窗口
      if (pdfUrl) {
        const newWindow = window.open(pdfUrl, '_blank');
        if (!newWindow) {
          this.$message.error('无法打开新窗口，请检查浏览器设置');
        }
      }
    }
  }
}
</script>
